package com.wyclabs.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyclabs.commons.request.UserCreateRequest;
import com.wyclabs.commons.request.UserUpdateRequest;
import com.wyclabs.commons.response.UserResponse;
import com.wyclabs.commons.utils.BeanUtils;
import com.wyclabs.user.mapper.UserMapper;
import com.wyclabs.user.model.User;
import com.wyclabs.user.service.UserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * user - 用户 Service实现类
 * @date 2019-10-10
 */
@Service
@Slf4j
@AllArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private final UserMapper userMapper;

    @Override
    public void create(UserCreateRequest request) {
        User user = new User();
        user.setUserName(request.getUserName());
        user.setPassword(request.getPassword());
        // TODO 设置默认头像
        userMapper.insert(user);
    }

    @Override
    public void update(UserUpdateRequest request) {
        User user = BeanUtils.copy(request, User.class);
        userMapper.updateById(user);
    }

    @Override
    public UserResponse findById(Long id) {
        return BeanUtils.copy(userMapper.selectById(id), UserResponse.class);
    }

    @Override
    public UserResponse findByUserName(String userName) {
        User user = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUserName, userName));
        return BeanUtils.copy(user, UserResponse.class);
    }
}
